package com.baizhi.administrator.controller;


import com.auth0.jwt.interfaces.DecodedJWT;
import com.baizhi.administrator.service.IAdminService;
import com.baizhi.model.entity.Admin;
import com.baizhi.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gange
 * @since 2023-07-17
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private IAdminService adminService;
    @PostMapping("/tokens")
    public Map<String,String> login(@RequestBody Admin admin){
        //判断是否登录成功
        String jwt = adminService.login(admin);
        Map<String,String> map = new HashMap<>();
        map.put("token",jwt);
        return map;
    }

    @GetMapping("/admin-user")
    public Map<String,String> adminUser(String token){
        DecodedJWT verify = JwtUtils.verify(token);
        String name = verify.getClaim("name").asString();
        String avatar = verify.getClaim("avatar").asString();
        Map<String,String> map = new HashMap<>();
        map.put("name",name);
        map.put("avatar",avatar);
        return map;
    }
}

